﻿@using Newtouch.Infrastructure;
@using Newtouch.Common.Operator;
@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
    var opeator = OperatorProvider.GetCurrent();
}
<style>
    /*input[type="checkbox"] {
        -webkit-appearance: none;
        vertical-align: middle;
        margin-top: 0;
        background: #fff;
        border: #999 solid 1px;
        border-radius: 3px;
        min-height: 12px;
        min-width: 12px;
        zoom:120%
    }
    input[type="checkbox"]:checked {    background: #3190e8;}
    input[type=checkbox]:checked::after {
        content: '';
        top: 3px;
        left: 3px;
        position: absolute;
        background: transparent;
        border: #fff solid 2px;
        border-top: none;
        border-right: none;
        height: 6px;
        width: 10px;
        -moz-transform: rotate(-45deg);
        -ms-transform: rotate(-45deg);
        -webkit-transform: rotate(-45deg);
        transform: rotate(-45deg);
    }*/


</style>


<form id="form1">
    <div class="widget-body">
        <div id="wizard" class="wizard" data-target="#wizard-steps" style="border-left: none; border-top: none; border-right: none;">
            <ul class="steps">
                <li data-target="#step-1" class="active"><span class="step">1</span>模板基本信息<span class="chevron"></span></li>
                <li data-target="#step-2"><span class="step">2</span>模板控制<span class="chevron"></span></li>
            </ul>
        </div>
        <div class="step-content" id="wizard-steps" style="border-left: none; border-bottom: none; border-right: none;">
            <div class="step-pane active" id="step-1" style="margin: 10px; margin-bottom: 0px;">
                @*<div class="alert alert-danger" style="text-align: left; margin-bottom: 10px;">
                    <i class="fa fa-warning alert-dismissible" style="position: relative; top: 1px; font-size: 15px; padding-right: 5px;"></i>
                    请维护模板相关信息，用于创建或修改病历文书！
                    </div>*@
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div style="width:20%;float:left;border-right:1px solid #b7d3f2;">
                            @Html.Partial("_BllxTree")
                        </div>
                        <div style="width:78%;float:right;">
                            <table class="form">
                                <tr>
                                    <td class="formTitle"><span class="required">*</span>业务类型：</td>
                                    <td class="formValue formDdlSelectorTd">
                                        <select id="mzbz" name="mzbz" class="form-control required">
                                            <option value="0">住院</option>
                                            <option value="1">门诊</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="formTitle"><span class="required">*</span>模板大类：</td>
                                    <td class="formValue formDdlSelectorTd">
                                        <input id="bllx" name="bllx" type="hidden" class="form-control required" readonly="readonly" />
                                        <input id="bllxmc" name="bllxmc" class="form-control required" readonly="readonly" />
                                    </td>
                                    <th class="formTitle"><span class="required">*</span>模板权限：</th>
                                    <td class="formValue formDdlSelectorTd">
                                        <select id="mbqx" name="mbqx" class="form-control required" data-EnumType="Enummbqx"></select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="formTitle"><span class="required">*</span>模板编码：</td>
                                    <td class="formValue"  colspan="3">
                                        <input id="mbbm" name="mbbm" type="text" class="form-control required" placeholder="请输入编码" />
                                    </td>
                                </tr>
                                <tr>
                                    <th class="formTitle"><span class="required">*</span>模板名称：</th>
                                    <td class="formValue" colspan="3">
                                        <input id="mbmc" name="mbmc" type="text" class="form-control required" placeholder="请输入模板名称" />
                                    </td>
                                </tr>
                                <tr>
                                    <th class="formTitle">所属科室：</th>
                                    <td class="formValue formDdlSelectorTd">
                                        <select id="ksbm" name="ksbm" class="form-control">
                                            <option value="">==请选择==</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <th class="formTitle">所属医生：</th>
                                    <td class="formValue formDdlSelectorTd">
                                        <input id="ysgh" name="ysgh" type="text" readonly="readonly" class="form-control" placeholder="请输入医生工号" />
                                    </td>
                                </tr>
                                <tr>
                                    <th class="formTitle"><span class="required">*</span>模板加载方式：</th>
                                    <td class="formValue formDdlSelectorTd">
                                        <select id="LoadWay" name="LoadWay" class="form-control required" data-EnumType="EnummbqxTempLoadWay"></select>
                                    </td>

                                </tr>
                                <tr>
                                    <th class="formTitle">模板路径：</th>
                                    <td class="formValue"  colspan="3">
                                        <input id="mblj" name="mblj" type="text" class="form-control required" placeholder="请输入模板路径" />
                                    </td>
                                </tr>
                                <tr>
                                    <th class="formTitle">是否为空：</th>
                                    <td class="formValue">
                                        <div class="ckbox">
                                            <input id="Isempty" name="Isempty" type="checkbox" checked="checked"><label for="Isempty">是</label>
                                        </div>
                                    </td>
                                    <th class="formTitle">是否上传医保：</th>
                                    <td class="formValue">
                                        <div class="ckbox">
                                            <input id="IsYB" name="IsYB" type="checkbox"><label for="IsYB">是</label>
                                        </div>
                                        <input id="Ybbm" name="Ybbm" type="text" class="form-control" style="width:50%;float:right;" placeholder="医保编码" />
                                    </td>
                                </tr>
                                <tr>
                                    <th class="formTitle" valign="top" style="padding-top: 5px;">
                                        引用路径：
                                    </th>
                                    <td class="formValue" colspan="3">
                                        <textarea id="Memo" name="Memo" class="form-control" style="height: 60px;" readonly="readonly"></textarea>
                                    </td>
                                </tr>
                                <tr>
                                    <th class="formTitle">启用数据加载：</th>
                                    <td class="formValue">
                                        <div class="ckbox">
                                            <input id="EnableDataLoad" name="EnableDataLoad" type="checkbox"><label for="EnableDataLoad">是</label>
                                        </div>
                                    </td>
                                    <th class="formTitle">绑定数据源：</th>
                                    <td class="formValue">
                                        <input id="DataSource" name="DataSource" type="text" class="form-control" placeholder="数据源" />
                                    </td>
                                </tr>
                                <tr>
                                    <th class="formTitle" style="height: 35px;">状态：</th>
                                    <td class="formValue" style="padding-top: 1px;">
                                        <div class="ckbox">
                                            <input id="zt" name="zt" type="checkbox" checked="checked"><label for="zt">有效</label>
                                        </div>
                                        <input id="OrganizeId" name="OrganizeId" type="hidden" />
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="step-pane" id="step-2" style="margin-left:5px;margin-right:5px;">
                <div class="alert alert-danger" style="text-align: left; margin-bottom: 5px;">
                    <i class="fa fa-warning alert-dismissible" style="position: relative; top: 1px; font-size: 15px; padding-right: 5px;"></i>
                    若需实施配置权限，请保证启用当前岗位权限控制。
                </div>
                <table id="gridList"></table>

            </div>
        </div>
        <div class="form-button" id="wizard-actions">
            <a id="btn_last" disabled class="btn btn-default btn-prev">上一步</a>
            <a id="btn_next" class="btn btn-default btn-next">下一步</a>
            <a id="btn_finish" class="btn btn-default" style="display: none;" onclick="submitForm()">完成</a>
        </div>
        @*<input id="ck_edit" type="checkbox" checked="checked" onclick="setCheck()" disabled="disabled" />*@
    </div>
</form>

<script>
    var keyValue = $.request("keyValue");
    var ly = $.request("ly");
    var bllx = $.request("bllx");
    var blId = $.request("blId");
    var mbly = $.request("mbly");
    var blmblj = "";

    $(function () {
        debugger;
        initControl();
        if (!!keyValue) {
            $.najax({
                url: "/MedicalRecordManage/MedRecordTemplate/GetFormJson",
                data: { keyValue: keyValue, blId: blId, bllx: bllx },
                dataType: "json",
                async: false,
                success: function (data) {
                    $("#form1").formSerialize(data);
                }
            });
        }
        else if (!!blId && !!bllx && mbly == 0) {
            $.najax({
                url: "/MedicalRecordManage/MedRecordTemplate/GetFormJson",
                data: { blId: blId, bllx: bllx, mbly: mbly },
                dataType: "json",
                async: false,
                success: function (data) {
                    $("#form1").formSerialize(data);
                }
            });

            document.getElementById("mbqx").disabled = true;
            document.getElementById("ysgh").readOnly = true;
            document.getElementById("bllx").disabled = true;
            document.getElementById("mblj").disabled = true;

        }


    });

    function initControl() {
        //组织机构下拉框
        //$("#bllx").bindSelect({
        //    url: "/DocumentManage/PatInfo/GetMedRecordType"
        //});

        //组织对应的科室下拉框加载
        $("#ksbm").bindSelect({
            url: "/MedicalRecordManage/MedRecordTemplate/GetSysDept"
        });

        $('#wizard').wizard().on('change', function (e, data) {
            var $finish = $("#btn_finish");
            var $next = $("#btn_next");
            if (data.direction == "next") {
                switch (data.step) {
                    case 1:
                        if (!$('#form1').formValid()) {
                            return false;
                        }
                        $finish.show();
                        $next.hide();
                        break;
                    default:
                        break;
                }
            } else {
                $finish.hide();
                $next.show();
            }
        });
        gridList();
        bllxTreeView("", bllx,null);
    }

    $('#mbqx').change(function () {
        checkqx()
    });

    function checkqx() {
        if ($('#mbqx').val() == '@((int)Enummbqx.prv)') {
            $('#ysgh').val('@opeator.rygh');
        }

    }

   
    $('#mbbm').focus(function () {
        debugger;
        var organizeId = $("#OrganizeId").val();
        if (!!!keyValue && ($.trim($(this).val()) === '' || organizeId != anPrevOrg)) {
            anPrevOrg = organizeId;
            setmbbm();
        }
    });
    var anPrevOrg = null;
    function setmbbm()
    {
        $.najax({
            url: "/HOME/GetNewFieldUniqueValue?topOrgIdIsStar=false&initFieldLength=5&fieldName=bl_mblb.mbbm&orgId=" + $("#OrganizeId").val() + "&r=" + Math.random(),
            dataType: "json",
            cache: false,
            success: function (data) {
                debugger;
                $('#mbbm').val(data.data);
            }
        });
    }

    //控制关系加载
    function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: "/MedicalRecordManage/MedRecordTemplate/GetblmbDuty",
            postData: { mbId: keyValue },
            height: $(window).height() - 200,
            colModel: [
                { label: '关系主键', name: 'Id', hidden: true },
                { label: '岗位主键', name: 'dutyCode', hidden: true },
                { label: '岗位名称', name: 'dutyName', width: 160, align: 'center' },
                {
                    label: "启用", name: "zt", width: 100, align: "center", formatter: function (cellvalue, options, rowobject) {
                        return cellvalue == '1' ? '<i class=\"fa fa-toggle-on\"  onclick="ctrlEnable(' + cellvalue + ',' + options.rowId + ')"></i>' : '<i class=\"fa fa-toggle-off\"  onclick="ctrlEnable(' + cellvalue + ',' + options.rowId + ')"></i>';
                    }
                },
                {
                    label: '只读权限', name: 'ctrlLevel', width: 150, align: 'center',
                    formatter: function (val, options, rowObject) {
                        if (val == '@((int)EnummbqxFp.read)') {
                            if (rowObject.zt == 0) {
                                return '<input name="ck_read_' + options.rowId + '" type="checkbox"  checked="checked" disabled="disabled" onclick="setCheckRead(' + options.rowId + ')" />';
                            }
                            else {
                                return '<input name="ck_read_' + options.rowId + '" type="checkbox"  checked="checked" onclick="setCheckRead(' + options.rowId + ')" />';
                            }
                        }
                        else {
                            if (rowObject.zt == 0) {
                                return '<input name="ck_read_' + options.rowId + '" type="checkbox" disabled="disabled" onclick="setCheckRead(' + options.rowId + ')" />';

                            }
                            else {
                                return '<input name="ck_read_' + options.rowId + '" type="checkbox" onclick="setCheckRead(' + options.rowId + ')" />';

                            }
                        }
                    }
                },
                {
                    label: '读写权限', name: 'ctrlLevel', width: 150, align: 'center',
                    formatter: function (val, options, rowObject) {
                        if (val == '@((int)EnummbqxFp.edit)') {
                            if (rowObject.zt == 1) {
                                return '<input name="ck_edit_' + options.rowId + '" type="checkbox"  checked="checked" onclick="setCheckEdit(' + options.rowId + ')" />';
                            }
                            else {
                                return '<input name="ck_edit_' + options.rowId + '" type="checkbox"  checked="checked" disabled="disabled" onclick="setCheckEdit(' + options.rowId + ')" />';
                            }
                        }
                        else {
                            if (rowObject.zt == 1) {
                                //if (!keyValue &&(rowObject.dutyCode == "Nurse" || rowObject.dutyCode == "doctor")) {

                                //}
                                return '<input name="ck_edit_' + options.rowId + '" type="checkbox" onclick="setCheckEdit(' + options.rowId + ')" />';
                            }
                            else {
                                return '<input name="ck_edit_' + options.rowId + '" type="checkbox" disabled="disabled" onclick="setCheckEdit(' + options.rowId + ')" />';
                            }
                        }
                    }
                }
                //{ label: '创建人', name: 'CreatorCode', width: 100, align: 'left' },
                //{ label: '创建时间', name: 'CreateTime', width: 130, align: 'left', formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' } },
                //{ label: '最后修改', name: 'LastModifierCode', width: 100, align: 'left' },
                //{ label: '最后修改时间', name: 'LastModifyTime', width: 130, align: 'left', formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' } },
            ],
            viewrecords: true
        });
    }

    function ctrlEnable(val, rowId) {
        //debugger
        //启用则可选
        if (val == 1) {
            $("#gridList").find("input[name='ck_read_" + rowId + "']").attr("disabled", true);
            $("#gridList").find("input[name='ck_edit_" + rowId + "']").attr("disabled", true);
            $("#gridList").jqGrid('setCell', rowId, "zt", 0);
        }
        else {
            $("#gridList").find("input[name='ck_read_" + rowId + "']").attr("disabled", false);
            $("#gridList").find("input[name='ck_edit_" + rowId + "']").attr("disabled", false);
            $("#gridList").jqGrid('setCell', rowId, "zt", 1);
        }
    }
    //setval 目的值
    function setCheckRead(rowId) {
        var checked = $("#gridList").find("input[name='ck_read_" + rowId + "']").is(':checked');
        if (checked)//选中当前
        {
            $("#gridList").find("input[name='ck_edit_" + rowId + "']").attr("checked", false);
            $("#gridList").jqGrid('setCell', rowId, "ctrlLevel", '@((int)EnummbqxFp.read)');
        }
    }

    function setCheckEdit(rowId) {
        var checked = $("#gridList").find("input[name='ck_edit_" + rowId + "']").is(':checked');
        if (checked) //选中当前
        {
            $("#gridList").find("input[name='ck_read_" + rowId + "']").attr("checked", false);
            $("#gridList").jqGrid('setCell', rowId, "ctrlLevel", '@((int)EnummbqxFp.edit)');
        }
    }

    function submitForm() {
        if (!$('#form1').formValid()) {
            return false;
        }

        if ($('#mbqx').val() == '@((int)Enummbqx.dept)' && $('#ksbm').val() == "") {
            $.modalMsg('请选择科室', 'error');
            return false;
        }
        if ($("#IsYB").prop("checked") && !$('#Ybbm').val()) {
            $.modalMsg('请输入医保编码', 'error');
            return false;
        }
        if ($("#EnableDataLoad").prop("checked") && !$('#DataSource').val()) {
            $.modalMsg('请输入数据源', 'error');
            return false;
        }
        var postData = $("#form1").formSerialize();
        $.submitForm({
            url: "/MedicalRecordManage/MedRecordTemplate/SubmitForm?keyValue=" + keyValue + "&mbly=" + mbly,
            param: postData,
            success: function (data) {
                if (data.state == "success") {
                    AcceptClick(); //保存权限
                    $.modalMsg('保存成功', 'success');
                }
                else {
                    $.modalMsg(data.message, 'error');
                }
                if (!ly) {
                    $.currentWindow().$("#gridList").resetSelection();
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
                else {
                    $.currentWindow().$('#mainIframeleft').attr('src', '/MedicalRecordManage/MedRecordTemplate/LeftMedicalTemplate');
                    //$.currentWindow().trigger();
                }
               
            }
        });

    }


    function AcceptClick(callBack) {
        var para = [];
        var lev = 0;
        var ids = $("#gridList").jqGrid('getRowData_AllLine');
        for (var i = 0; i < ids.length; i++) {
            if ($("#gridList").find("input[name='ck_read_" + ids[i].jqRowId + "']").is(':checked')) {
                lev = '@((int)EnummbqxFp.read)';
            }
            if ($("#gridList").find("input[name='ck_edit_" + ids[i].jqRowId + "']").is(':checked')) {
                lev = '@((int)EnummbqxFp.edit)';
            }

            var zt = "0"
            if (($("#gridList").jqGrid("getCell", ids[i].jqRowId, "zt").indexOf("fa-toggle-on")) != -1) {
                zt = "1";
            }

            if (lev.toString() != "0") {
                var item = { gxId: ids[i].Id, dutyCode: ids[i].dutyCode, dutyName: ids[i].dutyName, ctrlLevel: lev.toString(), zt: zt };
                para.push(item);
            }

            lev = 0;
        }
        var list = JSON.stringify(para);

        $.najax({
            url: "/MedicalRecordManage/MedRecordTemplate/SubmitAssigned",
            data: { list: list, mbId: keyValue, mbbm: $('#mbbm').val() },
            dataType: "json",
            async: false,
            success: function (data) {
                //$("#form1").formSerialize(data);
            }
        });

    }
    $('#mbmc').change(function () {
        var mbmc=$("#mbmc").val()+".xml";
        $("#mblj").val(blmblj + mbmc);
    });
    //双击病历大类处理
    function treeProcess(id) {
        debugger;
        if (!!keyValue) {
            $.modalMsg('模板大类不可修改', 'error');
        }
        else {
            blmblj = "\\File\\Template\\"
            $.najax({
                url: "/SystemManage/Common/GetBllxbyId",
                data: { keyValue: id },
                dataType: "json",
                async: false,
                success: function (data) {
                    debugger;
                    $("#form1").formSerialize(data);
                    if (data.ParentId) {
                        $.najax({
                            url: "/SystemManage/Common/GetBllxbyId",
                            data: { keyValue: data.ParentId },
                            dataType: "json",
                            async: false,
                            success: function (ref) {
                                debugger;
                                blmblj = blmblj + ref.bllxmc + "\\" + data.bllxmc + "\\" ;
                            }
                        });
                    }
                    else {
                        blmblj = blmblj + data.bllxmc + "\\";
                    }
                    if ($("#mbmc").val())
                        $("#mblj").val(blmblj + $("#mbmc").val()+".xml");
                    else
                        $("#mblj").val(blmblj);
                }
            });
            setmbbm();
        }
    }
</script>

